<template>
  <div class="profile-view">
    <div class="profile-container">
      <div class="profile-header">
        <div class="profile-avatar-section">
          <el-avatar :size="80" :src="userProfile?.avatar" class="profile-avatar">
            {{ username?.charAt(0)?.toUpperCase() || 'U' }}
          </el-avatar>
          <div class="profile-info">
            <h2>{{ username || '用户' }}</h2>
            <p>{{ $t('profile.userRole') }}</p>
            <div class="profile-stats">
              <div class="stat">
                <span class="stat-number">{{ stats.totalChats }}</span>
                <span class="stat-label">{{ $t('profile.stats.totalChats') }}</span>
              </div>
              <div class="stat">
                <span class="stat-number">{{ stats.favoriteCharacters }}</span>
                <span class="stat-label">{{ $t('profile.stats.favoriteCharacters') }}</span>
              </div>
              <div class="stat">
                <span class="stat-number">{{ stats.usageDays }}</span>
                <span class="stat-label">{{ $t('profile.stats.usageDays') }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>

      <div class="profile-content">
        <div class="profile-section" v-if="showUserStats">
          <h3>📊 {{ $t('profile.stats.title') }}</h3>
          <div class="stats-grid">
            <div class="stat-card">
              <div class="stat-icon">💬</div>
              <div class="stat-info">
                <h4>{{ $t('profile.stats.todayChats') }}</h4>
                <p>{{ stats.todayChats }} 次</p>
              </div>
            </div>
            <div class="stat-card">
              <div class="stat-icon">⭐</div>
              <div class="stat-info">
                <h4>{{ $t('profile.stats.mostUsedCharacter') }}</h4>
                <p>{{ stats.mostUsedCharacter || $t('profile.stats.none') }}</p>
              </div>
            </div>
            <div class="stat-card">
              <div class="stat-icon">📚</div>
              <div class="stat-info">
                <h4>{{ $t('profile.stats.mostUsedNovel') }}</h4>
                <p>{{ stats.mostUsedNovel || $t('profile.stats.none') }}</p>
              </div>
            </div>
          </div>
        </div>

        <div class="profile-section" v-if="showAccountStatus">
          <h3>👤 {{ $t('profile.account.title') }}</h3>
          <div class="account-status-grid">
            <div class="status-card">
              <div class="status-icon">✅</div>
              <div class="status-info">
                <h4>{{ $t('profile.account.emailVerified') }}</h4>
                <p class="status-verified">{{ $t('profile.account.verified') }}</p>
              </div>
            </div>
            <div class="status-card">
              <div class="status-icon">🔒</div>
              <div class="status-info">
                <h4>{{ $t('profile.account.security') }}</h4>
                <p class="status-secure">{{ $t('profile.account.secure') }}</p>
              </div>
            </div>
            <div class="status-card">
              <div class="status-icon">⭐</div>
              <div class="status-info">
                <h4>{{ $t('profile.account.membership') }}</h4>
                <p class="status-member">{{ $t('profile.account.regular') }}</p>
              </div>
            </div>
          </div>
        </div>



        <!-- 编辑模式 -->
        <div class="profile-section" v-if="showEditMode">
          <h3>✏️ {{ $t('profile.edit.title') }}</h3>
          <div class="edit-form">
            <el-form :model="editForm" label-width="120px">
              <el-form-item :label="$t('profile.edit.nickname')">
                <el-input v-model="editForm.nickname" :placeholder="$t('profile.edit.nickname')" />
              </el-form-item>
              <el-form-item :label="$t('profile.edit.bio')">
                <el-input 
                  v-model="editForm.bio" 
                  type="textarea" 
                  :rows="3"
                  :placeholder="$t('profile.edit.bioPlaceholder')"
                />
              </el-form-item>
              <el-form-item :label="$t('profile.info.phone')">
                <el-input v-model="editForm.phone" :placeholder="$t('profile.info.phone')" />
              </el-form-item>
              <el-form-item :label="$t('profile.info.gender')">
                <el-radio-group v-model="editForm.gender">
                  <el-radio label="male">{{ $t('profile.info.male') }}</el-radio>
                  <el-radio label="female">{{ $t('profile.info.female') }}</el-radio>
                  <el-radio label="other">{{ $t('profile.info.other') }}</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item :label="$t('profile.info.birthday')">
                <el-date-picker
                  v-model="editForm.birthday"
                  type="date"
                  :placeholder="$t('profile.info.birthday')"
                  format="YYYY-MM-DD"
                  value-format="YYYY-MM-DD"
                />
              </el-form-item>
              <el-form-item :label="$t('profile.info.location')">
                <el-input v-model="editForm.location" :placeholder="$t('profile.info.location')" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="handleSaveProfile">{{ $t('profile.edit.save') }}</el-button>
                <el-button @click="handleCancelEdit">{{ $t('profile.edit.cancel') }}</el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted, reactive } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import type { UserProfile } from '../../types/user'

interface UserStats {
  totalChats: number
  todayChats: number
  favoriteCharacters: number
  usageDays: number
  mostUsedCharacter: string
  mostUsedNovel: string
}

interface UserSettings {
  speechSpeed: number
  autoPlay: boolean
  emotionLevel: number
  replyLength: string
  language: string
  theme: string
  themeColor: string
}
interface Props {
  username?: string
  userProfile?: UserProfile | null
  stats: UserStats
  settings: UserSettings
}

defineProps<Props>()

const emit = defineEmits<{
  updateSetting: [key: string, value: any]
}>()

// 路由实例
const route = useRoute()
const router = useRouter()

// 计算当前模式
const currentMode = computed(() => {
  return route.query.mode || 'default'
})

// 根据模式决定显示哪些部分
const showUserStats = computed(() => currentMode.value === 'default')
const showAccountStatus = computed(() => currentMode.value === 'default')
const showEditMode = computed(() => currentMode.value === 'edit')

// 编辑表单数据
const editForm = reactive({
  nickname: '',
  bio: '',
  phone: '',
  gender: '',
  birthday: '',
  location: ''
})

// 编辑相关方法
const handleSaveProfile = () => {
  console.log('保存个人资料:', editForm)
  // 这里应该调用API保存数据
  router.back()
}

const handleCancelEdit = () => {
  router.back()
}


</script>

<script lang="ts">
export default {
  name: 'ProfileView'
}
</script>

<style scoped>
.profile-view {
  padding: 40px;
  height: 100%;
  overflow-y: auto;
}

.profile-container {
  max-width: 800px;
  margin: 0 auto;
}

.profile-header {
  margin-bottom: 40px;
}



.profile-avatar-section {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 30px;
  background: white;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.profile-avatar {
  background: var(--primary-gradient);
  color: white;
  font-size: 32px;
  font-weight: 700;
}

.profile-info h2 {
  font-size: 24px;
  font-weight: 700;
  color: #1f2937;
  margin-bottom: 4px;
}

.profile-info p {
  color: #6b7280;
  margin-bottom: 20px;
}

.profile-stats {
  display: flex;
  gap: 24px;
}

.stat {
  text-align: center;
}

.stat-number {
  display: block;
  font-size: 24px;
  font-weight: 700;
  color: var(--primary-color);
  margin-bottom: 4px;
}

.stat-label {
  font-size: 12px;
  color: #6b7280;
}

.profile-content {
  display: flex;
  flex-direction: column;
  gap: 30px;
}

.profile-section h3 {
  font-size: 20px;
  font-weight: 700;
  color: #1f2937;
  margin-bottom: 20px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
}

.stat-card {
  background: white;
  padding: 24px;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  display: flex;
  align-items: center;
  gap: 16px;
}

.stat-icon {
  font-size: 32px;
  flex-shrink: 0;
}

.stat-info h4 {
  font-size: 16px;
  font-weight: 600;
  color: #1f2937;
  margin-bottom: 4px;
}

.stat-info p {
  font-size: 14px;
  color: #6b7280;
  margin: 0;
}



/* 账号状态样式 */
.account-status-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
  margin-bottom: 30px;
}

.status-card {
  background: white;
  padding: 24px;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  display: flex;
  align-items: center;
  gap: 16px;
}

.status-icon {
  font-size: 32px;
  flex-shrink: 0;
}

.status-info h4 {
  font-size: 16px;
  font-weight: 600;
  color: #1f2937;
  margin-bottom: 4px;
}

.status-info p {
  font-size: 14px;
  margin: 0;
}

.status-verified {
  color: #10b981;
  font-weight: 600;
}

.status-secure {
  color: #059669;
  font-weight: 600;
}

.status-member {
  color: #667eea;
  font-weight: 600;
}



/* 编辑表单样式 */
.edit-form {
  max-width: 600px;
  margin: 0 auto;
}

.edit-form .el-form {
  background: white;
  padding: 24px;
  border-radius: 12px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .profile-avatar-section {
    flex-direction: column;
    text-align: center;
  }
  
  .profile-stats {
    justify-content: center;
  }
  
  .stats-grid,
  .account-status-grid {
    grid-template-columns: 1fr;
  }
}
</style> 